How to recover your EC2 instance key pair

Web Development
Updated at: Sep 30, 2023
Be

BadgerBloke

me@badgerbloke.com

If you are looking for exact way to regenerate or redownload the already generated key-pair of your AWS EC2 instance then, I will apologize and would like to let you know in very beginning of this article that, I haven't found any such way. However, if you want to have a new EC2 key-pair without losing any data of your EC2 instance, then this article will definitely help you.

How to recover your EC2 instance key pair

Hi there! If you are looking for exact way to regenerate or redownload the already generated key-pair of your AWS EC2 instance then, I will apologize and would like to let you know in very beginning of this article that, I haven't found any such way.

However, if you want to have a new EC2 key-pair without losing any data of your EC2 instance, then this article will definitely help you.

👉🏻 Time Saver >>> In case you already have good experience with AWS. Then you can create an AMI, and then you can launch an instance from that AMI. Here will not lose anything and get a new key-pair as well.

Here, I'm going to mention the process in a step-wise manner by putting an example. So, in case someone is referring this for learning purpose only, even then he can get most out of this reading.

Old EC2 instance (marvel) - I will treat it like its key-pair has been lost

1.png image 1

You can see in the above image, I have a directory named test which contains two files named test.py and test.sh

Create an AMI (Amazon Machine Images) of existing EC2 instance

  1. Open EC2 services then, go to instances section
  2. Select required instance
  3. Click on Actions dropdown button
  4. Go to Image and templates
  5. Select Create image and it will take you to the AMI creation flow, like image 3 below 2.png image 2

AMI creation flow

  1. Provide Image name then,
  2. Provide Image description (optional)
  3. You can go with default options for rest of the steps, or you can include/exclude your additional volumes. 3.png image 3

You can add tags also to your image. Now, move ahead by clicking on Create Image button and it will take you back to the instances page. You will get a message for your AMI creation status, like - image 5

4.png image 4

5.png image 5

If I check my AMI's status then, it will show as pending because my instance is running.

6.png image 6

Now, I have to stop my instance. So, that AMI will get created

  1. Go to instances
  2. Select required instance
  3. Open Instance State dropdown
  4. Select Stop instance

7.png image 7

Now, on the AMI page you will find your AMI's updated status

Once your AMI's status will become available then it can be used for launching new instances. Until AMI's status becomes available, please do not make any modification to your EC2 instance. (Obviously you have to select the required one in case you have multiple AMIs)

8.png image 8

You can launch an instance by clicking on the "Launch instance from AMI" button in the top right corner, or you can go to EC2 >>> instances >>> Launch an instance. Here you will find "My AMIs" tab alongside "Quick Start" tab under "Application and OS images" section. simply select the respective image as mentioned in image 9

9.png image 9

Under "Key pair" section, you have to create a new Key Pair

10.png image 10

Simply put a name for your key pair

(Generally I go with "Key pair type" = "RSA" and "Private key file format" = ".pem". However, you can proceed as per your wish) Now, click on "Create key pair" and it will download the "private key file".

11.png image 11

Now launch the instance

11 1.png image 12

You can see in below image 13 a new instance has been created and initializing. (If only 2 instances confusing you then let me tell you, in the beginning of this article I had 2 instances one of them was terminated so by the time that one gone. That's why even after creating a new instance you are seeing only two instead of 3)

12.png image 13

Now let me show you that my data from older instance (marvel) has been carried to this instance or not

13.png image 14

All the data has been carried successfully.

Congrats 👏🏻 Now you will be able to do the same...